package munyorderweb_Android;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import munyorderweb_DB.DB_manager;

public class SamplePostServlet extends HttpServlet {

	@Override
	protected void doPost(
			HttpServletRequest req,
			HttpServletResponse resp)
					throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");

		Connection con = null;
		try{
			con = DB_manager.getConnection();
			Statement stm = con.createStatement();

			Calendar calendar = Calendar.getInstance();
			int hour = calendar.get(Calendar.HOUR_OF_DAY);

			//		1	リクエスト情報から、端末番号、オーダー番号、テーブル番号を取得する
			//		2	リクエスト情報から、メニュー番号、注文数を全て取得する

			Map<String, String[]> map = req.getParameterMap();

			String[] Ca_na = (String[])map.get("category_name");
			String[] Ca_no = (String[])map.get("category_no");
			String[] Ta_no = (String[])map.get("table_no");
			String[] Ta_na = (String[])map.get("table_name");
			String[] Ta_se = (String[])map.get("table_seats");
			String[] Me_no = (String[])map.get("menu_no");
			String[] Me_na = (String[])map.get("menu_name");
			String[] Me_pr = (String[])map.get("menu_price");
			String[] Or_no = (String[])map.get("order_no");
			String[] Or_co = (String[])map.get("order_count");
			String[] Or_de = (String[])map.get("order_delivery");
			String[] Cu_no = (String[])map.get("customer_no");
			String[] Cu_to = (String[])map.get("customer_total");
			String[] Cu_pe = (String[])map.get("customer_person");
			String[] Cu_me = (String[])map.get("customer_member");

			//			上記項目のいずれかが取得できない場合、処理結果として「SC_BAD＿REQEST（４００）」を返して終了する。
			if (Cu_to.length == 0 || Cu_pe.length == 0
					|| Me_no.length == 0 || Cu_me.length == 0
					|| Ta_no.length == 0 || Cu_no.length == 0
					|| Or_co.length == 0)
			{
				resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			}
			else {
				for(int i=0; i<Ca_na.length; i++){

					String Ca_Na  = Ca_na[i];
					String Ca_No  = Ca_no[i];
					String Ta_No  = Ta_no[i];
					String Ta_Na  = Ta_na[i];
					String Ta_Se  = Ta_se[i];
					String Me_No  = Me_no[i];
					String Me_Na  = Me_na[i];
					String Me_Pr  = Me_pr[i];
					String Or_No  = Or_no[i];
					String Or_Co  = Or_co[i];
					String Or_De  = Or_de[i];
					String Cu_No  = Cu_no[i];
					String Cu_To  = Cu_to[i];
					String Cu_Pe  = Cu_pe[i];
					String Cu_Me  = Cu_me[i];

					//				注文数が正数でない場合、処理結果として「SC＿BAD_REQEST（４００）」を返して終了する。
					if(Integer.parseInt(Or_Co) <= 0){
						resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
					}else{

						System.out.println("Key :  " + Ca_Na);
						System.out.println("Key :  " + Ca_No);
						System.out.println("Key :  " + Ta_No);
						System.out.println("Key :  " + Ta_Na);
						System.out.println("Key :  " + Ta_Se);
						System.out.println("Key :  " + Me_No);
						System.out.println("Key :  " + Me_Na);
						System.out.println("Key :  " + Me_Pr);
						System.out.println("Key :  " + Or_No);
						System.out.println("Key :  " + Or_Co);
						System.out.println("Key :  " + Or_De);
						System.out.println("Key :  " + Cu_No);
						System.out.println("Key :  " + Cu_To);
						System.out.println("Key :  " + Cu_Pe);
						System.out.println("Key :  " + Cu_Me);

						//				ordersテーブルに情報を追加する。
						String sql = "insert into customer( customer_total, customer_person, order_date,"
								+ " customer_time, customer_member)"
								+ " values( "
								+ Cu_To + "," + Cu_Pe + ", now(), "+ hour + ", " + Cu_Me + ") ";
						stm.executeUpdate(sql);
						sql = "insert into orders( table_no, menu_no, order_count, order_delivery, customer_no)"
								+ " values( "
								+ Ta_No + ", " + Me_No + "," + Or_Co + ", 0, " + Cu_No +")";
						stm.executeUpdate(sql);
						//			例外が発生した場合、「SC_SERVICE_UNAVAILABLE（５０３）」を返して終了する。
					}
				}
			}
		}catch(SQLException e){
			e.printStackTrace();
			resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
		}finally{
			try{
				if(con != null){
					con.close();
				}
			}catch (SQLException e){
				e.printStackTrace();
			}
		}

		//	3	処理結果として「SC＿OK（２００）」を返して終了する。
		resp.setStatus(HttpServletResponse.SC_OK);
	}
}